<template>
    <div class="User-container content-wrapper">
        <div class="filter-wrapper">
            <el-form ref="filterForm" :model="listQuery" :inline="true">

                <span class="margin-right-5">角色：{{this.$route.query.roleName}}</span>

                <el-form-item prop="realName" size="medium">
                    <el-input v-model="listQuery.realName" placeholder="姓名"></el-input>
                </el-form-item>

                <el-form-item prop="account" size="medium">
                    <el-input v-model="listQuery.account" placeholder="账号"></el-input>
                </el-form-item>

                <el-form-item prop="sex" size="medium">
                    <el-select v-model="listQuery.sex" placeholder="性别" clearable style="width: 100px">
                        <el-option
                                v-for="item in sexOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="status" size="medium">
                    <el-select v-model="listQuery.status" placeholder="状态" clearable style="width: 100px">
                        <el-option
                                v-for="item in statusOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="orderBy" size="medium">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" clearable style="width: 120px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <div class="toolbar">
                    <el-button type="primary" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
                    <el-button type="primary" size="medium" icon="el-icon-error" @click="handleResetFilter">重置</el-button>
                    <el-button type="danger" size="medium" icon="el-icon-delete" @click="handleBatchDelete">移除</el-button>
                </div>
            </el-form>
        </div>

        <el-table
                :data="list"
                border fit
                highlight-current-row
                v-loading="listLoading"
                size="medium"
                element-loading-text="给我一点时间"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55" align="center">
            </el-table-column>
            <el-table-column label="ID" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="姓名" width="110px" align="center">
                <template slot-scope="scope"> {{scope.row.realName}} </template>
            </el-table-column>

            <el-table-column label="性别" width="80px" align="center">
                <template slot-scope="scope">
                    <el-tag type="primary" v-if="scope.row.sex === 1">男</el-tag>
                    <el-tag type="danger" v-else-if="scope.row.sex === 2">女</el-tag>
                    <el-tag type="info" v-else>未知</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="账号" width="110px" align="center">
                <template slot-scope="scope"> {{scope.row.account}} </template>
            </el-table-column>

            <el-table-column label="手机号码" width="130px" align="center">
                <template slot-scope="scope"> {{scope.row.mobile}} </template>
            </el-table-column>

            <el-table-column label="邮件地址" min-width="200px" align="center" show-overflow-tooltip>
                <template slot-scope="scope"> {{scope.row.email}} </template>
            </el-table-column>

            <el-table-column label="状态" width="80px" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
                    <el-tag type="danger" v-else>禁用</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.ctime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>
            <el-table-column label="最后修改时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.utime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="操作" width="110px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">移除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
    import { selectList } from '@/api/upms/user'
    import { removeRoleUsers } from '@/api/upms/role'

    const sexOptions = [{label: '男', key: 1}, {label: '女', key: 2}, {label: '未知', key: 3}]
    const sortOptions = [{label: '按ID升序', key: 'id asc'}, {label: '按ID降序', key: 'id desc'}]
    const statusOptions = [{label: '正常', key: 1}, {label: '锁定', key: 2}]

    export default {
        name: 'User',
        data() {
            return {
                list: [],
                total: null,
                listLoading: false,
                multipleSelection: [],
                listQuery: {
                    page: 1,
                    rows: 10,
                    roleId: this.$route.query.roleId,
                    account: undefined,
                    realName: undefined,
                    status: undefined,
                    sex: undefined,
                    orderBy: 'id asc'
                },
                sexOptions,
                statusOptions,
                sortOptions

            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            handleFilter() {//条件过滤
                this.listQuery.page = 1
                this.getTableData()
            },
            handleResetFilter() {//重置过滤条件
                this.$refs.filterForm.resetFields()
                this.getTableData()
            },
            handleSelectionChange(val) {//多选
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getTableData()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getTableData()
            },
            handleDelete(row) {//删除数据
                this.$confirm('确定要移除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    removeRoleUsers({ids: row.userRoleAssignId}).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getTableData()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            handleBatchDelete() {//批量删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(this.multipleSelection.length > 0) {
                        let ids = []
                        for(let item of this.multipleSelection) {
                            ids.push(item.userRoleAssignId)
                        }
                        removeRoleUsers({ids: ids.join(',')}).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })

                    } else {
                        this.$notify({
                            title: '提示', message: '请选择需要删除的记录',
                            type: 'warning', duration: 2000
                        })
                    }
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            getTableData() {
                if(this.listQuery.roleId) {
                    this.listLoading = true
                    selectList(this.listQuery).then(response => {
                        this.list = response.data.list
                        this.total = response.data.total
                        this.listLoading = false
                    }).catch(error => {
                        console.log(error)
                        this.listLoading = false
                    })
                } else {
                    this.$message({ type: 'error', message: '请点击群组管理，再查看详情' })
                }
            }

        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>